<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>后台布局</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
</head>
<body>
<!--添加功能的form-->
<form class="layui-form" action="" id="insertLostForm"
      style="display: none;margin-right: 40px;margin-top: 20px">

    <div class="layui-form-item">
        <label class="layui-form-label">失主</label>
        <div class="layui-input-block">
            <input type="text" name="luid" autocomplete="off" placeholder="失主"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">遗失物品</label>
        <div class="layui-input-block">
            <input type="text" name="lcontent" autocomplete="off" placeholder="您的遗失物品"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">遗失房间</label>
        <div class="layui-input-block">
            <select name="llid" id="addLostSelect">

            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit id="insertLostButton" lay-filter="insertLostFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<!--编辑功能的form-->
<form class="layui-form" action="" id="updateLostForm" lay-filter="updateLostForm"
      style="display: none;margin-right: 40px;margin-top: 20px">

    <input type="hidden" name="lid">
    <div class="layui-form-item">
        <label class="layui-form-label">失主</label>
        <div class="layui-input-block">
            <input type="text" name="luid" autocomplete="off" placeholder="失主"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">遗失物品</label>
        <div class="layui-input-block">
            <input type="text" name="lcontent" autocomplete="off" placeholder="您的遗失物品"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">遗失房间</label>
        <div class="layui-input-block">
            <select name="llid" id="editEquipmentSelect">

            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updateLostFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<!--顶部搜索框-->
<form class="layui-form" action="" style="margin-top: 10px">
    <div class="layui-inline">
        <label class="layui-form-label">失主:</label>
        <div class="layui-input-inline">
            <input type="text" name="luid" autocomplete="off" placeholder="失主" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">遗失物品:</label>
        <div class="layui-input-inline">
            <input type="text" name="lcontent" autocomplete="off" placeholder="遗失物品" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">遗失房间:</label>
        <div class="layui-input-inline">
            <select name="llid" id="searchLostSelect">

            </select>
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="searchFilter">搜索</button>
            <button type="reset" class="layui-btn layui-btn-primary" id="myButton">重置</button>
        </div>
    </div>
</form>
<table id="showLost" lay-filter="lostFilter"></table>
</body>
<!--顶部工具栏-->
<script type="text/html" id="lostToolbar">
    <div class="layui-btn-container">
        <button  class="layui-btn layui-btn-xs" lay-event="addLost">
            <i class="layui-icon layui-icon-add-1"></i>添加
        </button>
        <button  class="layui-btn layui-btn-danger layui-btn-xs" lay-event="deleteLosts">
            <i class="layui-icon layui-icon-delete"></i>删除
        </button>
    </div>
</script>
<!--实现部门管理操作功能按钮-->
<script type="text/html" id="lostBar">
    <a  class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
    <a  class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script>
    layui.use(['table', 'layer', 'jquery', 'form'], function () {
        let table = layui.table;
        let layer = layui.layer;
        let $ = layui.jquery;
        let form = layui.form;

        let lostTable = table.render({
            elem: '#showLost'
            , url: '/lost/selectAll' //数据接口
            , toolbar: '#lostToolbar' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: []
            , page: true//开启分页
            , limit: 5
            , limits: [5, 10, 15, 20]
            , title: 'lostTable'
            , cols: [
                [ //表头
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'luid', title: '失主', width: 100}
                    , {field: 'lcontent', title: '遗失物品', width: 100}
                    , {field: 'rname', title: '遗失房间', width: 100}
                    , {field: 'ltime', title: '遗失上报时间', width: 200}
                    , {fixed: 'right', align: 'center', title: '操作', toolbar: '#lostBar', width: 120}
                ]
            ]
        });
        $.ajax({
            url: "/room/getAllRoomNoPage",
            type: "get",
            dataType: "json",
            success: function (result) {
                let options =  ' <option value="0">请选择</option>';
                for (let i = 0; i < result.data.length; i++) {
                    options += '<option  value="' + result.data[i].rid + '">' + result.data[i].rname + '</option>';
                }
                $("#addLostSelect")  .html(options);
                $("#editLostSelect").html(options);
                $("#searchLostSelect").html(options);
                layui.use('form', function(){
                    var form = layui.form;
                    form.render();
                });

            }
        });
        //监听行工具事件
        table.on('tool(lostFilter)', function (obj) {
            let data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del') {
                var idList=[];
                //遍历数组data，抽取所有的id
                idList.push(data.lostId);
                layer.confirm('真的删除行么', function (index) {


                    $.ajax({
                        url: "/lost/delete",
                        type: "delete",
                        dataType: "JSON",
                        data: {idList:idList},
                        success: function (ret) {
                            if (ret.code == 0) {
                                //刷新表格数据
                                lostTable.reload({
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                });
                                layer.msg(ret.msg, {icon: 6});
                            } else {
                                layer.msg(ret.msg, {icon: 5});
                            }
                        }
                    });
                    layer.close(index);
                    //向服务端发送删除指令
                });
            } else if (layEvent === 'edit') {

                form.val('updateLostForm', {
                    'lid': data.lid,
                    'luid': data.luid,
                    'lcontent': data.lcontent,
                    'llid': data.llid,
                });
                layer.open({
                    title: '更改遗失物品信息',
                    type: 1,
                    area: ['500px', '500px'],
                    content: $("#updateLostForm")//这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
            }
        });

        //form表单实现更新功能
        form.on('submit(updateLostFilter)', function (data) {

            //提交表单数据
            $.ajax({
                url: "/lost/update",
                type: "PUT",
                dataType: "JSON",
                contentType:"application/json;charset=UTF-8",
                data:JSON.stringify(data.field),
                success: function (ret) {
                    if (ret.code == 0) {
                        //关闭对话框
                        layer.closeAll();
                        //刷新表格数据
                        lostTable.reload({
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                        layer.msg(ret.msg, {icon: 6});
                    } else {
                        layer.msg(ret.msg, {icon: 5});
                    }
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //监听头工具栏事件
        table.on('toolbar(lostFilter)', function (obj) {
            let checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'addLost':
                    document.getElementById("insertLostForm").reset();
                    layer.open({
                        title: '添加遗失物品清单',
                        type: 1,
                        area: ['500px', '450px'],
                        content: $("#insertLostForm")
                    });
                    break;
                case 'deleteLosts':
                    if (data.length == 0) {
                        layer.msg('请选择一行');
                    } else {
                        var idList=[];
                        //遍历数组data，抽取所有的id
                        for (let i = 0; i < data.length; i++) {
                            idList[i]=(data[i].lid);
                        }
                        layer.confirm('真的删除行么', function () {
                            //for循环收集所有选中的id

                            $.ajax({
                                url: "/lost/delete",
                                type: "delete",
                                dataType: "JSON",
                                data:{idList:idList},
                                success: function (ret) {
                                    if (ret.code == 0) {
                                        //刷新表格数据
                                        lostTable.reload({
                                            page: {
                                                curr: 1 //重新从第 1 页开始
                                            }
                                        });
                                        layer.msg(ret.msg, {icon: 6});
                                    } else {
                                        layer.msg(ret.msg, {icon: 5});
                                    }
                                }
                            });
                        });
                    }
                    break;
            }
        });

        //form表单实现添加功能
        form.on('submit(insertLostFilter)', function (data) {

            //提交表单数据

            $.ajax({
                url: "/lost/insert",
                type: "POST",
                dataType: "JSON",
                contentType:"application/json;charset=UTF-8",
                data:JSON.stringify(data.field),
                success: function (ret) {
                    if (ret.code == 0) {
                        //关闭对话框
                        layer.closeAll();
                        //刷新表格数据
                        lostTable.reload({
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                        layer.msg(ret.msg, {icon: 6});
                    } else {
                        layer.msg(ret.msg, {icon: 5});
                    }
                }
            });


            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //实现顶部搜索框
        form.on('submit(searchFilter)', function (data) {
            //执行重载
            lostTable.reload({
                where: {
                    luid: data.field.luid,
                    lcontent: data.field.lcontent,
                    llid: data.field.llid,

                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //重置条件查询表单，刷新table数据
        $("#myButton").click(function () {
            lostTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    lid: ""
                    , luid: ""
                    , lcontent: ""
                    , llid: ""

                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
    });
</script>
</html>